﻿@model GroupModel
@if (ViewBag.IsPjaxRequest == false)
{
    Layout = "../Shared/__Layout.cshtml";
}
<section class="content-header">
    <h1>
        编辑工作组
    </h1>
    <ol class="breadcrumb">
        <li><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i> 首页</a></li>
        <li>用户</li>
        <li class="active">工作组管理</li>
    </ol>
</section>

<section class="content">

    <div class="box box-primary">
        <div class="box-body">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#jibenxinxi" data-toggle="tab">基本信息</a>
                </li>
                <li id="glry">
                    <a href="#guanlirenyuan" data-toggle="tab">包含用户</a>
                </li>

            </ul>
            <div class="tab-content">
                <div class="tab-pane active fade in" id="jibenxinxi">
                    <div id="modalcontent">
                        @using (Html.Bootstrap().BeginForm(c => c.Horizontal()))
                        {
                            @Html.ValidationSummary()
                            <div class="form-group">
                                @Html.LabelFor(m => m.Title, new { @class = "col-sm-2 control-label" })
                                <div class="col-sm-10">
                                    @Html.Bootstrap().TextBoxFor(m => m.Title)
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(m => m.Remark, new { @class = "col-sm-2 control-label" })
                                <div class="col-sm-10">
                                    @Html.Bootstrap().TextareaFor(m => m.Remark)
                                </div>
                            </div>
                            @Html.HiddenFor(x => x.Id)
                        }
                    </div>

                </div>
                <div class="tab-pane fade" id="guanlirenyuan">
                    <div class="dataTable-tools btn-group">
                        @Html.Bootstrap().LinkButton("添加", ToolBarIco.Create).Href(Url.Action("UserDialog", new { groupId = Model.Id })).Attribute("data-toggle", "ajaxModal").Attribute("data-search", "#selectedUsers")

                        @Html.Bootstrap().LinkButton("删除", ToolBarIco.Delete).Click("UnRelationUser('" + @Url.Action("UnRelationUser", new { groupId = Model.Id }) + "')").Route(Url.Action("Delete"))
                    </div>
                    <div class="table-responsive">
                        <table id="selectedUsers" width="100%" class="table table-bordered table-striped table-condensed table-hover"></table>
                    </div>
                </div>
            </div>
            @if (!Model.UI.CheckReadOnlyForm())
            {
                <div class="box-footer text-center box-outfooter">
                    @Html.Bootstrap().Button("保存").Style(BootstrapStyle.Primary).Click("window.app.saveForm(event,'" + Url.Action("Save") + "');").Ico(ToolBarIco.Save)
                    @Html.Bootstrap().Button("返回").Style(BootstrapStyle.Default).Click("window.app.loadUI('" + Url.Action("Index") + "');").Ico(ToolBarIco.Back)
                </div>
            }
        </div>
    </div>

</section>


<script>
    $(function () {
        $('#selectedUsers').dataTable({
            "serverSide": true,
            select: { style: 'multi', selector: 'td:first-child' },
            "ajax": {
                "url": "@Url.Action("GetUserPaged")",
                "type": "POST",
                "data": {
                    groupId: $("#Id").val(),
                    includeGroup: true
                }
            },
            "columnDefs": [
                 { sDefaultContent: '', aTargets: ['_all'] }
            ],
            columns: [
                { title: '<!--<label class="checkbox i-checks"><input type="checkbox" /><i></i></label>-->', data: null, sortable: false, width: 30, "sClass": 'checkColumn select-checkbox' },
                { title: '账号', data: 'userName', width: 70, "sClass": "center" },
                { title: "姓名", data: 'person.fullName', width: 160, "sClass": "left" },
                {
                    title: "性别", data: 'person.genderCode', render: function (data, type, row, meta) {
                        var genderCode;
                        if (row.person) genderCode = row.person.genderCode;
                        else genderCode = "";
                     return app.code2Text("Gender", genderCode);
                },  "sClass": "left" }
            ]
        });
    });
    UnRelationUser = function(url) {

        var userIds = new Array();
        var list = $("#selectedUsers").DataTable().rows({ selected: true }).data();
        $.each(list, function (index, item) {
            userIds.push(item.id);
        });

        if (userIds.length === 0) {
            window.app.message("请选中一项。");
            return;
        }

        swal({
            title: "请确认是否删除？",
            text: "您正在进行删除操作，该操作不可逆!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger"
        }).then(function(isConfirm) {
            if (isConfirm === true) {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { "userIds": userIds },
                    dataType: "json",
                    success: function(data) {
                        if (data.errors.length > 0) {
                            swal("出错啦！", data.errors, "error");
                        } else {
                            swal("已经删除", "删除成功！", "success");
                            $("#selectedUsers").DataTable().ajax.reload();
                        }
                    }
                });
            }
        });

    };
</script>